//
//  QRSelectController.m
//  EagleEye
//
//  Created by 18_李明明 on 15-1-29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "QRSelectController.h"
#import "DataBaseHandler.h"
#import "QrObject.h"
#import "WebQRController.h"
@interface QRSelectController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) DataBaseHandler *dataBase;
@property (nonatomic, strong) NSMutableArray *contentArr;
@end

@implementation QRSelectController
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.contentArr = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataBase = [DataBaseHandler shareInstance];
    // Do any additional setup after loading the view.
    [self.dataBase openDB];
    self.contentArr = [self.dataBase selectQR];
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, -35, WIDTH, HEIGHT - 30) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableview];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
}
- (void)makeView
{
    if (self.contentArr.count == 0) {
        UILabel *label = [[UILabel alloc] initWithFrame:self.tableview.frame];
        [self.tableview addSubview:label];
        label.text = @"您还没有扫描过二维码/条形码，快去扫描吧!\nO(∩_∩)O~";
        label.numberOfLines = 0;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    [self makeView];
    return self.contentArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"0502"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"0502"];
    }
    QrObject *qr = [self.contentArr objectAtIndex:indexPath.row];
    cell.textLabel.text = qr.content;
    return cell;
}
// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    NSNotificationCenter *noti = [[NSNotificationCenter alloc]init];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    WebQRController *web = [[WebQRController alloc] init];
    web.result = cell.textLabel.text;
    [self.navigationController pushViewController:web animated:YES];
    
    
//    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:cell.textLabel.text, @"name", nil];
//    [noti postNotificationName:@"QR" object:nil userInfo:dic];
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [self.dataBase deleteQRWithContent:cell.textLabel.text];
    [self.contentArr removeObjectAtIndex:indexPath.row];
    [self.tableview reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
